//
//  KPCityTestViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/2.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import SwifterSwift
import HandyJSON
import SwiftyJSON

class KPCityTestViewController: KPBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        KPLog("----------------开始解析-------------------")
        let path = Bundle.main.path(forAuxiliaryExecutable: "cityString.json")

        let data = try! Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
        var json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [Any]

        let array = JSONDeserializer<CityModel>.deserializeModelArrayFrom(array: json)
        let jsonArray = array!.compactMap { $0?.toJSON() }

        let filePath = NSHomeDirectory() + "/Documents/cityplist.plist"
        (jsonArray as NSArray).write(toFile: filePath, atomically: true)

    }

    struct CityModel: HandyJSON {
        var code: String!
        var name: String!
        var citylist = [RegionEntitysModel]()

    }

    struct RegionEntitysModel: HandyJSON {
        var pinyin: String!
        var first: String!
        var code: String!
        var name: String!
        var arealist = [RegionModel]()

        mutating func didFinishMapping() {
            pinyin = name.transformToPinYin()
            first = pinyin.first?.uppercased.string
        }
    }

    struct RegionModel: HandyJSON {
        var code: String!
        var name: String!

    }

}
